package com.xkstream.client.presenter.kt

import com.xkstream.client.api.UserApi
import com.xkstream.client.api.UserService
import com.xkstream.client.base.BaseData
import com.xkstream.client.base.BasePresenterImpl
import com.xkstream.client.base.BaseView
import com.xkstream.client.entity.kt.GainsData
import com.xkstream.client.entity.kt.GiftRankingData
import com.xkstream.client.rx.RxHelper
import com.xkstream.client.rx.RxObserver
import com.xkstream.client.utils.LogUtil
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import java.util.*

/**
 * Created by xiaoyi on 2017-11-2.
 */
class GainsRecorderPresenter() : BasePresenterImpl(), GainsRecorderContract.Presenter {


    var view: GainsRecorderContract.View? = null
    var userService: UserService? = null

    constructor(view: GainsRecorderContract.View) : this() {
        this.view = view
        this.view?.setPresenter(this)
        this.userService = UserApi.getInstance().service
    }

    override fun getGiftRanking(params: Map<String, Any>) {
        view?.showLoading()
        userService?.getGiftRanking(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<GiftRankingData>>(this) {
                    override fun onSuccess(t: BaseData<GiftRankingData>) {
                        view?.hideLoading()
                        view?.showGiftRanking(t)
                    }

                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }
                })
    }

    override fun getTodayGains(params: Map<String, Any>) {
        view?.showLoading()
        userService?.getTodayGains(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<GainsData>>(this) {
                    override fun onSuccess(t: BaseData<GainsData>) {
                        view?.hideLoading()
                        view?.showTodayGains(t)
                    }

                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }
                })
    }

    override fun getGainsData(params: Map<String, Any>) {
        view?.showLoading()

        Observable.zip(userService?.getTodayGains(params),
                userService?.getGiftRanking(params),

                object : BiFunction<BaseData<GainsData>, BaseData<GiftRankingData>, GainsData> {
                    override fun apply(gainsDataBaseData: BaseData<GainsData>, data: BaseData<GiftRankingData>): GainsData {
                        val gainsData = gainsDataBaseData.data
                        gainsData.ranking = data.data
                        return gainsData
                    }
                })
                .compose(RxHelper.toUI())
                .subscribe(object :RxObserver<GainsData>(this){
                    override fun onSuccess(t: GainsData) {
                        view?.hideLoading()
                        view?.showGainsData(t)
                    }

                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }
                })

    }


}